package com.ms.game.invitefriend.async;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;

import com.ms.game.invitefriend.db.DistributorInviteCodeDAO;
import com.ms.game.invitefriend.db.DistributorInviteCodesModel;
import com.ms.game.invitefriend.db.DistributorsModel;
import com.ms.game.invitefriend.db.InviteFriendComSumLogModel;
import com.ms.game.invitefriend.db.SubDistributorsModel;
import com.ms.game.invitefriend.message.DownlineCommInfo;
import com.ms.game.invitefriend.message.SGetDownlineCommInfoResponse;
import com.ms.player.Player;
import com.ms.player.PlayerIdentityType;
import com.ms.service.error.ErrorCode;
import com.xpec.c4.db.dao.PlayerDAO;
import com.xpec.c4.game.utility.GUID;
import com.xpec.c4.service.message.MessageAction;
import com.xpec.c4.service.module.task.TaskAsyncCommand;

public class GetDownlineCommInfoAsync extends TaskAsyncCommand {

	public Player player;

	/**
	 * 子經銷商ID
	 */
	public GUID subDistributorGUID;
	
	/**
	 * 一頁幾筆
	 */
	public int maxPerPage;
	
	/**
	 * 第幾頁 
	 */
	public int page;
	

	/**
	 * MessageTypes.S_INVITE_CODE_GET_DOWNLINE_COMM_RESPONSE;
	 */
	public int messageType;

	public GUID sessionId;

	@Override
	public void asyncExecute() throws Exception {
		SGetDownlineCommInfoResponse response = new SGetDownlineCommInfoResponse();
		
		Player subDistributorPlayerData = PlayerDAO.loadPlayerByGuid(subDistributorGUID);
		if(subDistributorPlayerData == null) {
			response.setErrorCode(ErrorCode.DATA_NOT_FOUND);
			MessageAction.sendMessage(sessionId, messageType, response);
			return;
		}

		int identifyType = player.getPlayerIdentityType();
		SubDistributorsModel subDistributorsModel = null;
		if(identifyType == PlayerIdentityType.DISTRIBUTOR || identifyType == PlayerIdentityType.BOTH_DISTRIBUTOR_DOWNLINE){
			//來者是經銷商
			
			//檢查子經銷商的model在不在
			subDistributorsModel = DistributorInviteCodeDAO.getSubDistributorsModelByGuid(subDistributorPlayerData.getGuid().toString());
			//如果沒有或者是 model中經銷商guid 不等於 經銷商guid
			if(subDistributorsModel == null || !subDistributorsModel.getDistributorGuid().equals(player.getGuid().toString())){
				response.setErrorCode(ErrorCode.DATA_NOT_FOUND);
				MessageAction.sendMessage(sessionId, messageType, response);
				return;
			}
		}else if(identifyType == PlayerIdentityType.SUB_DISTRIBUTOR){
			//子經銷商
			
			//要檢查自己是不是子經銷商
			subDistributorsModel = DistributorInviteCodeDAO.getSubDistributorsModelByGuid(player.getGuid().toString());
			if(subDistributorsModel == null || !subDistributorsModel.getDistributorGuid().equals(player.getUplineId())){
				response.setErrorCode(ErrorCode.DATA_NOT_FOUND);
				MessageAction.sendMessage(sessionId, messageType, response);
				return;
			}
		}else{
			response.setErrorCode(ErrorCode.DATA_NOT_FOUND);
			MessageAction.sendMessage(sessionId, messageType, response);
			return;
		}
		
		DistributorsModel distributorsModel = DistributorInviteCodeDAO.getDistributorModelByGUID(subDistributorsModel.getDistributorGuid());
		if(distributorsModel == null || !distributorsModel.isEffective()){
			response.setErrorCode(ErrorCode.DATA_NOT_FOUND);
			MessageAction.sendMessage(sessionId, messageType, response);
			return;
		}
		
		String subDistributorGUID = subDistributorsModel.getSubDistributorGuid().toString();
		
		int beginPos = (page - 1) * maxPerPage;
		if(page == 1){
			//第一次要給他總頁數
			int totalPage =  DistributorInviteCodeDAO.getTotalDownlineCommInfoPage(identifyType, subDistributorGUID, maxPerPage);
			response.setTotalPage(totalPage);
		}
		
		//撈出指定頁數的資料
		ArrayList<DistributorInviteCodesModel> inviteFriendComSumLogModels = DistributorInviteCodeDAO.getDownlineCommInfoByPage(subDistributorGUID, identifyType, beginPos, maxPerPage);
		LinkedList<String> guids = new LinkedList<String>();
		for (DistributorInviteCodesModel model : inviteFriendComSumLogModels) {
			if (model.getIdentifyType() == PlayerIdentityType.DISTRIBUTOR) {
				//使用經銷商邀請碼的 子經銷商們
				guids.add(model.getSubDistributorGuid());
			} else if (model.getIdentifyType() == PlayerIdentityType.SUB_DISTRIBUTOR) {
				//使用子經銷商邀請碼的 下線們
				guids.add(model.getDownLineGuid());
			}
		}
		
		//拿等級
		HashMap<String, Integer> playerLevelMap = PlayerDAO.getPlayerGuidLevelMap(guids);
		
		//計算抽傭總額
		LinkedList<DownlineCommInfo> infos = new LinkedList<DownlineCommInfo>();
		if(guids.size() > 0){
			HashMap<String, InviteFriendComSumLogModel> playerComSumMap = DistributorInviteCodeDAO.getPlayerComSumsByGuidList(guids, player.getGuid().toString());
			for (String guid : guids) {
				InviteFriendComSumLogModel model = playerComSumMap.get(guid);
				DownlineCommInfo info = new DownlineCommInfo();
				info.setName(model.getDownLineName());
				info.setLevel(playerLevelMap.get(guid));
				info.setTotalComm(model.getComValue().intValue());
				infos.add(info);
			}
		}
		
		response.setDownlineCommInfos(infos);
		response.setErrorCode(ErrorCode.SUCCESS);
		MessageAction.sendMessage(sessionId, messageType, response);
	}

	@Override
	public void execute() throws Exception {

	}

}
